using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using NUnit.Framework;
using Com.ContentRendering.Api.RenderEngine;
using Com.ContentRendering.Api.RenderEngineComponentTest.Helper;
using System.Text;

namespace Com.ContentRendering.Api.RenderEngineComponentTest
{
    [TestFixture]
    public class ComponentTest
    {
        private Dictionary<string, string> _resources;

        [TestFixtureSetUp]
        public void SetUpFixture()
        {
            this._resources = new Dictionary<string, string>();

            Assembly assembly = Assembly.GetExecutingAssembly();

            foreach (string typeFullName in assembly.GetManifestResourceNames())
            {
                using (Stream stream = assembly.GetManifestResourceStream(typeFullName))
                {
                    using (TextReader textReader = new StreamReader(stream))
                    {
                        this._resources.Add(typeFullName, textReader.ReadToEnd());
                    }
                }
            }
        }

        private DocumentRender _documentRender;

        [SetUp]
        public void SetUp()
        {
            this._documentRender = new DocumentRender();
        }

        [Test]
        public void ContentTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.Content.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                null);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.Content.txt"],
                stringBuilder.ToString());
        }

        [Test]
        public void ContentTwiceTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.ContentTwice.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                null);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.ContentTwice.txt"],
                stringBuilder.ToString());
        }

        [Test]
        public void TagRootOutputTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();
            XmlDocument dataXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.TagRootOutput.xml"]);
            dataXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Data.TagRootOutput.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            InputDataInterface inputDataHelper = new InputDataHelper(dataXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                inputDataHelper);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.TagRootOutput.txt"],
                stringBuilder.ToString());
        }

        [Test]
        public void TagRootSubstitutionTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();
            XmlDocument dataXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.TagRootSubstitution.xml"]);
            dataXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Data.TagRootSubstitution.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            InputDataInterface inputDataHelper = new InputDataHelper(dataXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                inputDataHelper);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.TagRootSubstitution.txt"],
                stringBuilder.ToString());
        }

        [Test]
        public void TagRootIfNotOneTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();
            XmlDocument dataXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.TagRootIfNotOne.xml"]);
            dataXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Data.TagRootIfNotOne.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            InputDataInterface inputDataHelper = new InputDataHelper(dataXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                inputDataHelper);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.TagRootIfNotOne.txt"],
                stringBuilder.ToString());
        }

        [Test]
        public void TagRootIfNotTwoTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();
            XmlDocument dataXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.TagRootIfNotTwo.xml"]);
            dataXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Data.TagRootIfNotTwo.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            InputDataInterface inputDataHelper = new InputDataHelper(dataXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                inputDataHelper);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.TagRootIfNotTwo.txt"],
                stringBuilder.ToString());
        }

        [Test]
        public void TagComplexOneTest()
        {
            XmlDocument nodeXmlDocument = new XmlDocument();
            XmlDocument dataXmlDocument = new XmlDocument();

            nodeXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Node.TagComplexOne.xml"]);
            dataXmlDocument.LoadXml(this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Input.Data.TagComplexOne.xml"]);

            InputNodeInterface inputNodeHelper = new InputNodeHelper(nodeXmlDocument.DocumentElement);
            InputDataInterface inputDataHelper = new InputDataHelper(dataXmlDocument.DocumentElement);
            StringBuilder stringBuilder = new StringBuilder();

            // render the document
            this._documentRender.Render(
                new StringWriter(stringBuilder),
                inputNodeHelper,
                inputDataHelper);

            Assert.AreEqual(
                this._resources["Com.ContentRendering.Api.RenderEngineComponentTest.Test.Output.TagComplexOne.txt"],
                stringBuilder.ToString());
        }
    }
}
